package com.zb.frame.modules.monitor.service.impl;

import com.alibaba.fastjson.JSON;
import com.zb.frame.modules.monitor.entity.Log;
import com.zb.frame.modules.monitor.enums.LogModule;
import com.zb.frame.modules.monitor.enums.LogType;
import com.zb.frame.modules.monitor.mapper.LogMapper;
import com.zb.frame.modules.monitor.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-09
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Autowired
    LogMapper logMapper;

    @Override
    public boolean addLog(LogModule module, LogType type, String oldValue, String newValue, String relId, Long operator, String msg) {
        Log info = new Log();
        info.setLogModule(module.getDesc());
        info.setLogType(type.getType());
        info.setOldValue(oldValue);
        info.setNewValue(newValue);
        info.setRelId(relId);
        info.setOperator(operator);
        info.setBatchFlag(0);
        info.setReturnFlag(0);
        info.setOkFlag(1);
        info.setFailMsg("");
        info.setLogDesc(msg);
        return this.save(info);
    }

    @Override
    public boolean addLogError(LogModule module, LogType type, String oldValue, String newValue, String relId, Long operator, String errorReason, String msg) {
        Log info = new Log();
        info.setLogType(type.getType());
        info.setOldValue(oldValue);
        info.setNewValue(newValue);
        info.setRelId(relId);
        info.setOperator(operator);
        info.setBatchFlag(0);
        info.setReturnFlag(0);
        info.setOkFlag(0);
        info.setFailMsg(errorReason);
        info.setLogDesc(msg);
        return this.save(info);
    }

    @Override
    public boolean addLog(List<Log> list) {
        return this.saveBatch(list);
    }

    @Override
    public List<Log> getLogList(Integer page, Integer limit, Map<String, Object> params){
        Integer start = (page - 1) * limit;
        Integer end = page * limit;
        params.put("start", start);
        params.put("end", end);
        return logMapper.getLogList(params);
    }

    @Override
    public Long getLogListCount(Map<String, Object> params){
        return logMapper.getLogListCount(params);
    }
}
